package com.ace.slide.data;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;

import com.ace.slide.model.Member;

public class MemberDAO extends BaseDAO {

	public static Member getById(long id) {
		return getById(Member.class, id);
	}
	public static List<Member> getAll()  {
		return getAll(Member.class);
	}

	public static Member authenticate(String username, String password) {
		Calendar cal = Calendar.getInstance();
		Query qry = session
				.createQuery(
						"from Member m where m.username = :name and m.password = :pass")
				.setString("name", username).setString("pass", password);
		Member m = (Member) qry.uniqueResult();
		if (m != null)  {
			m.getProfile().setLastLogin(cal.getTime());
			save(m);
		}
		
		return m;
	}
	
	public static boolean isUnique(Member m)  {
		boolean isUnique = false;
		Query qry = session
		.createQuery(
				"from Member m where m.username = :name and m.password = :pass")
		.setString("name", m.getUsername()).setString("pass", m.getPassword());
		
		if(qry.uniqueResult() != null)  {
			isUnique = false;
		} else isUnique = true;
		
		return isUnique;
	}
}
